package com.wangsong.system.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangsong.common.model.CodeEnum;
import com.wangsong.common.model.CustomUserDetails;
import com.wangsong.common.model.GetEasyUIData;
import com.wangsong.common.util.PasswordUtil;
import com.wangsong.system.entity.Resources;
import com.wangsong.system.entity.User;
import com.wangsong.system.entity.UserRole;
import com.wangsong.system.mapper.UserMapper;
import com.wangsong.system.mapstruct.UserMapstruct;
import com.wangsong.system.service.IResourcesService;
import com.wangsong.system.service.IRoleService;
import com.wangsong.system.service.IUserService;
import com.wangsong.system.vo.UserAddVO;
import com.wangsong.system.vo.UserPageVO;
import com.wangsong.system.vo.UserUpdateVO;
import com.wangsong.system.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-09-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IResourcesService resourcesService;
    @Autowired
    private UserMapstruct userMapstruct;
    @Override
    public GetEasyUIData list(UserPageVO user) {
        IPage<User> page = new Page<>(user.getPage(), user.getRows());
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StrUtil.isNotBlank(user.getUsername()), "username", user.getUsername());
        IPage<User> page1 = page(page, queryWrapper);
        return new GetEasyUIData(page1.getRecords(), page1.getTotal());
    }

    @Override
    @Transactional
    public void add(UserAddVO user) {
        Long[] roleIds = user.getRoleId();

        user.setPassword(PasswordUtil.md5DigestAsHex(user.getPassword()));
        save(user);

        roleService.saveBatch(roleIds, user);
    }


    @Override
    @Transactional
    public void updateUser(UserUpdateVO user) {
        Long[] roleIds = user.getRoleId();


        if (StrUtil.isNotBlank(user.getPassword())) {
            user.setPassword(PasswordUtil.md5DigestAsHex(user.getPassword()));
        } else {
            user.setPassword(null);
        }
        updateById(user);
        roleService.removeUserRole(user);
        roleService.saveBatch(roleIds, user);
    }

    @Override
    @Transactional
    public void deleteUser(Long[] id) {
        removeByIds(ListUtil.toList(id));
        roleService.removeUserRoles(id);
    }


    @Override
    public void updatePassword(UserAddVO user) {
        if (StrUtil.isNotBlank(user.getPassword())) {
            user.setPassword(PasswordUtil.md5DigestAsHex(user.getPassword()));
        } else {
            user.setPassword(null);
        }
        updateById(user);
    }

    @Override
    public Boolean findTByT(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        User userOne = getOne(queryWrapper);
        if (userOne == null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public User findTByUsername(String userDetails) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userename", userDetails);
        User userOne = getOne(queryWrapper);
        userOne.setPassword("");
        return userOne;
    }


    @Override
    public UserVO selectVOByPrimaryKey(Long id) {
        User user = getById(id);
        user.setPassword("");
        UserVO userVO =userMapstruct.toVO(user);
        List<UserRole> userRoleList = roleService.list(id);
        userVO.setUserRoleList(userRoleList);
        return userVO;
    }

    @Override
    public User getOne(String userName) {
        QueryWrapper queryWrapper21 = new QueryWrapper();
        queryWrapper21.eq("username", userName);
        User one = getOne(queryWrapper21);
        return one;
    }

    @Override
    public UserDetails loadUserByUsername(String username)  {
        User user = getOne(username);

        if (user == null) {
            throw new UsernameNotFoundException("Could not find the user '" + username + "'");
        }
        List<UserRole> userRoleList = roleService.list(user.getId());
        Set<SimpleGrantedAuthority> simpleGrantedAuthorities = resourcesService.loadUserByUser(userRoleList);
        return new CustomUserDetails(user, true, true, true, true, simpleGrantedAuthorities);
    }

    @Override
    public List<Resources> findResourcesEMUByResources(String userName) {
        User one = getOne(userName);
        List<UserRole> userRoleList = roleService.list(one.getId());

        return resourcesService.findResourcesEMUByResources(userRoleList);
    }

}
